import entity.Baby;
import entity.Pet;
import entity.TypeEntity;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import service.PetService;

public class SpringTest {
    @Test
    public void testSpringIoC(){
        ApplicationContext context =
                new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        //通过applicationContext的getBean()方法，根据id类获取
        Pet pet = (Pet) context.getBean("pet");
        pet.introduce();
    }
    @Test
    public void testService(){
        ApplicationContext context =
                new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        //通过applicationContext的getBean()方法，根据id来获取bean的实例
        Pet pet = (Pet) context.getBean("pet");
        PetService petService = (PetService) context.getBean("petService");
        petService.save(pet);
    }
    @Test
    public void testConstructorDI(){
        ApplicationContext context =
                new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        Baby baby = (Baby) context.getBean("redBoy");
        baby.introduce();
    }
    @Test
    public void testTypeDI(){
        ApplicationContext context =
                new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        TypeEntity typeEntity = (TypeEntity) context.getBean("typeEntity");
        typeEntity.printAll();
    }
}
